package com.ly.lw.styledo.fragment;


import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;

import com.alibaba.fastjson.JSON;
import com.handmark.pulltorefresh.library.PullToRefreshStaggeredGridView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.ly.lw.styledo.R;
import com.ly.lw.styledo.adapter.CategoryInfoSecondAdapter;
import com.ly.lw.styledo.beancategorysecondinfo.SnapList;
import com.ly.lw.styledo.config.UrlCategory;
import com.ly.lw.styledo.utils.FileUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片详情
 * A simple {@link Fragment} subclass.
 */
public class CategorySecondPicFragment extends Fragment {

    private int prdNo;//传参

    @ViewInject(R.id.category_second_info_ptrsgv)
    PullToRefreshStaggeredGridView ptrsgv;
    private ArrayList<SnapList> datas;
    private CategoryInfoSecondAdapter adapter;

    private BitmapUtils bitmapUtils;

    public CategorySecondPicFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            prdNo = getArguments().getInt("prdNo");
        }
        initBitmap();
    }


    public static CategorySecondPicFragment newInstance(int prdNo) {
        CategorySecondPicFragment fragment = new CategorySecondPicFragment();
        Bundle args = new Bundle();
        args.putInt("prdNo", prdNo);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_category_second_pic, container, false);
        ViewUtils.inject(this,view);
        initView();
        initData();
        return view;
    }

    private void initView() {
        datas=new ArrayList<>();
        initShopUi();
    }

    private void initBitmap() {
        bitmapUtils = new BitmapUtils(getActivity().getApplicationContext(), FileUtils.getImageCache());
        bitmapUtils.configDefaultLoadFailedImage(R.mipmap.ic_logo_small);
        bitmapUtils.configDefaultLoadingImage(R.mipmap.ic_logo_small);

        AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
        animation.setDuration(1000);
        bitmapUtils.configDefaultImageLoadAnimation(animation);
        bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565);//设置图片压缩类型

        bitmapUtils.configMemoryCacheEnabled(true);
        bitmapUtils.configDiskCacheEnabled(true);
    }

    /**
     * 初始化瀑布流控件相关参数
     */
    private void initShopUi() {
//        ptrsgv.setMode(PullToRefreshBase.Mode.BOTH);
//        ptrsgv.setOnRefreshListener(this);
//        staggeredGridView = ptrsgv.getRefreshableView();
//        staggeredGridView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, false, true));
        adapter = new CategoryInfoSecondAdapter(datas, getActivity(), bitmapUtils);
        ptrsgv.setAdapter(adapter);
    }

    private void initData() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = getRequestParams();
        httpUtils.send(HttpRequest.HttpMethod.POST, UrlCategory.CATEGORY_SECOND_INFO, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                try {
                    JSONArray array = new JSONObject(result).optJSONObject("result").optJSONObject("product").optJSONArray("snap_shot_img_list");
//                    Log.i("info", "--------" + array.toString());
                    List<SnapList> list = JSON.parseArray(array.toString(), SnapList.class);
                    datas.clear();
                    datas.addAll(list);
                    adapter.notifyDataSetChanged();
//                    Log.i("info", "--------" + list.size());
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    private RequestParams getRequestParams() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("target_width", "600");
        params.addBodyParameter("prd_no", prdNo + "");
        params.addBodyParameter("appVersion", "1.4.6");
        params.addBodyParameter("os", "android");
        params.addBodyParameter("osVersion", "4.1.2");
        return params;
    }

}
